package com.lzj.server.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.lzj.server.admin.service.AdminService;
import com.lzj.server.constant.SystemConstant;
import com.lzj.server.constant.enums.SystemEnum;
import com.lzj.server.admin.entity.Permission;
import com.lzj.server.admin.entity.Role;
import com.lzj.server.entity.video.Category;
import com.lzj.server.exceptions.BaseException;
import com.lzj.server.repository.mapper.AdminMapper;
import com.lzj.server.repository.mapper.CategoryMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {
    @Resource
    private AdminMapper adminMapper;

    @Resource
    private CategoryMapper categoryMapper;

    /**
     * 添加角色
     */
    @Override
    public void addRole(Role role) {
        if (ObjUtil.isNotEmpty(role)) {
            adminMapper.addRole(role);
        } else {
            throw new IllegalArgumentException("角色为NULL或者角色属性不完整，新建失败");
        }
    }

    /**
     * 添加权限
     */
    @Override
    public void addPermission(Permission permission) {
        if (ObjUtil.isNotEmpty(permission)) {
            adminMapper.addPermission(permission);
        } else {
            throw new IllegalArgumentException("权限为NULL或者权限属性不完整，新建失败");
        }
    }

    /**
     * 查看全部权限
     */
    @Override
    public List<Permission> getPermissionList() {
        return adminMapper.getPermissionList();
    }

    /**
     * 查看全部角色
     */
    @Override
    public List<Role> getRoleList() {
        return adminMapper.getRoleList();
    }

    /**
     * 删除角色或者权限
     */
    @Override
    public void deleteRoleOrPermission(Long id, Integer typeNum) {
        if (SystemEnum.ROLE.code() == typeNum) {
            adminMapper.deleteRoleById(id);
        } else if (SystemEnum.PERMISSION.code() == typeNum) {
            adminMapper.deletePermissionById(id);
        } else {
            throw new BaseException("非法类型: " + typeNum);
        }
    }

    /**
     * 配置角色权限
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void configPermission(List<Long> permissionIdList, Long roleId) {
        if (CollUtil.isEmpty(permissionIdList)) {
            throw new IllegalArgumentException("权限ID不能为空");
        }
        // 1. 删除原有权限
        adminMapper.deleteRolePermission(roleId);
        // 2. 配置权限
        for (Long permissionId : permissionIdList) {
            adminMapper.configPermission(permissionId, roleId);
        }
    }

    /**
     * 配置用户角色
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void configRole(List<Long> roleIdList, Long userId) {
        if (CollUtil.isEmpty(roleIdList)) {
            throw new IllegalArgumentException("角色ID不能为空");
        }
        // 1. 删除原有角色
        adminMapper.deleteUserRole(userId);
        // 2. 配置角色
        for (Long roleId : roleIdList) {
            adminMapper.configUserRole(userId, roleId);
        }
    }

    @Override
    public Boolean addOrEditCategory(Category category) {
        Assert.notNull(category, "分类信息不能为空");
        Long id = category.getId();
        if (ObjUtil.isNull(id)) {
            // 新增分类
            category.setDescription(StrUtil.nullToDefault(category.getDescription(), "暂时没有"));
            category.setType(StrUtil.nullToDefault(category.getType(), SystemConstant.VIDEO_MODULE));
            category.setParentId(NumberUtil.nullToZero(category.getParentId()));
            category.setSort(NumberUtil.nullToZero(category.getSort()));
            category.setName(StrUtil.nullToDefault(category.getName(), "未知分类"));
            return categoryMapper.insert(category) > 0;
        } else {
            // 编辑分类
            Category c = categoryMapper.selectById(id);
            Assert.notNull(c, "分类信息不存在");
            BeanUtil.copyProperties(category, c, CopyOptions.create().setIgnoreNullValue(true));
            return categoryMapper.updateById(c) > 0;
        }
    }

    /**
     *  根据角色编号获取角色ID
     * @param roleCode  角色编号
     * @return 角色ID
     */
    @Override
    public Long getRoleIdByCode(String roleCode) {
        return adminMapper.getRoleIdByCode(roleCode);
    }

}
